﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class LeagueLoginController : BaseController<league>
    {
        //商家中心登陆  加盟者登陆信息
        // GET: /LeagueLogin/

        public override BLL.BaseBll<league> bll
        {
            get
            {
                return new LeagueBll();
            }

        }
        //加载数据列表 查询方法          //leagueid  leaguepassword leaguename   leaguephone leaguesex  leagueemail leagueadress  leaguenumber registertime status storeids
        public override ActionResult Get(int page, int rows, league model)
        {
            int count;
            var list = bll.Search(model, x => true, true, x => x.leagueid, rows, page, out count).ToList().Select(x => new
            {   
                x.leagueid,
                x.leaguename,
                x.leaguephone,
                leaguesex=x.leaguesex==0?"男":"女", // 
                x.leaguepassword,
                x.leagueemail ,
                x.leagueage,
                x.leagueadress,
                x.leaguenumber,
               // registertime=x.registertime.ToLongDateString(),//Value.ToString("yyyy-MM-dd"),
                x.storeids, // 
                status = x.status == 1 ? "有效" : "无效",
            }).ToList();
            return Json(new { total = count, rows = list });
        }

        StoreBll storeBll = new StoreBll();

        public class StroeModel
        {
            public int storeid;
            public string storeName;
            public string property;
            public string storeDress;
            public string discount;

        }
        List<StroeModel>  StroeModelList=new List<StroeModel>();
        // 获取该理货员 管理的 店铺列表 + 
        public ActionResult GetDetailStore(string storeids)
        {
            string[] arrids = storeids.Split(',');
            for (int i = 0; i < arrids.Length; i++)
            {
                int id = Convert.ToInt32(arrids[i]);
                var storeList = storeBll.Search(x => x.storeid == id && x.storeStatus == 1).Select(x => new { x.storeid, x.storeName, property = x.property == 1 ? "大号" : x.property == 2 ? "中号" : "小号", x.storeDress, x.discount }).ToList();
                if (storeList.Count!=0)
                {
                    StroeModel storeModel = new StroeModel();
                    storeModel.discount = storeList.FirstOrDefault().discount.ToString();
                    storeModel.property = storeList.FirstOrDefault().property;
                    storeModel.storeDress = storeList.FirstOrDefault().storeDress;
                    storeModel.storeid = storeList.FirstOrDefault().storeid;
                    storeModel.storeName = storeList.FirstOrDefault().storeName;
                    StroeModelList.Add(storeModel);
                }
           }
         return Json(StroeModelList);
        }


        //新增 加盟者  信息
        [HttpPost]  
        public override ActionResult Save(league model)
        {
            //storeids   ，10010，10011
            string msg = "";
            int result = 0;
            int jieguo = 1;
            string code = model.leaguephone;
            string pwd = model.leaguepassword;

            if (model.leaguephone != "" && model.leaguepassword != "")
            {
                var count = bll.Search(x => x.leaguephone == code).Count();
                //判断该用户是否存在
                if (count == 0)
                {
                    string  storeids=model.storeids.Remove(0, 1);
                    model.storeids = storeids;
                    model.leaguecardurl = "";
                    model.leaguenumber = 0;
                    model.registertime = DateTime.Now;
                    model.leaguepassword = MD5Encrypt32(pwd);
                    model.status = 1;
                    if (bll.Add(model) > 0)
                    {
                        msg = "添加成功";
                        result = 1;
                        jieguo = 1;
                        return Json(new { jieguo = jieguo, state = result, msg = msg });
                    }
                    else
                    {
                        jieguo = 1;
                        msg = "添加失败";
                        return Json(new { jieguo = jieguo, state = result, msg = msg });
                    }
                }
                else
                {
                    jieguo = 0;
                    msg = "该账号已经存在，请重新填写";
                    return Json(new { jieguo = jieguo, state = result, msg = msg });
                }
            }
            else
            {
                jieguo = 0;
                msg = "请填写完整";
                return Json(new { jieguo = jieguo, state = result, msg = msg });
            }
        }
        /// <summary>
        /// 修改相关供应商信息  多选框  过不来值   如果之前是空 就会有前面的 ，
        /// /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public override ActionResult Modify(league model)
        {
            string msg = "修改失败";
            string state = "false";
            model.leaguepassword = MD5Encrypt32(model.leaguepassword);
            string storeids = model.storeids.Remove(0, 1);
            model.storeids = storeids;
            string code = model.leaguephone;
            string pwd = model.leaguepassword;
            model.status = 1;
            model.leaguenumber = 0;
            model.leaguecardurl = "";
            model.registertime = bll.Search(x => x.leagueid == model.leagueid).Select(x => new { x.registertime }).FirstOrDefault().registertime;
            if (model.leaguephone != "" && model.leaguepassword != "")
            {
                if (bll.Modify(model) > 0)
                {
                    msg = "修改成功";
                    state = "true";
                }
            }
            else
            {
                msg = "请填写完整";
                return Json(new { state = state, msg = msg });
            }
            return Json(new { state = state, msg = msg });
        }


        //禁用用户  修改状态为
        public ActionResult ModifyState(int  id)
         {
            string msg = "禁用失败";
            string state = "false";

            var oneList = bll.Search(x => x.leagueid == id).Select(x => new { x.leagueid,x.leaguephone, x.leagueadress, x.leagueage, x.leaguecardurl, x.leagueemail, x.leaguename,x.leaguenumber,x.leaguepassword,x.leaguesex,x.registertime,x.storeids }).ToList();
            league model = new league();
            model.leagueid = oneList.FirstOrDefault().leagueid;
            model.leaguename = oneList.FirstOrDefault().leaguename;
            model.leaguepassword = oneList.FirstOrDefault().leaguepassword;
            model.leaguephone = oneList.FirstOrDefault().leaguephone;
            model.leaguesex = oneList.FirstOrDefault().leaguesex;
            model.leagueage = oneList.FirstOrDefault().leagueage;
            model.leagueadress = oneList.FirstOrDefault().leagueadress;           
            model.leaguecardurl = oneList.FirstOrDefault().leaguecardurl;
            model.leagueemail = oneList.FirstOrDefault().leagueemail;
            model.registertime = oneList.FirstOrDefault().registertime;
            model.storeids = oneList.FirstOrDefault().storeids;
            model.leaguenumber = oneList.FirstOrDefault().leaguenumber;
            model.status = 0;
            if (bll.Modify(model) > 0)
            {
                msg = "禁用成功";
                state = "true";
            }
            return Json(new { state = state, msg = msg });
        }

        public ActionResult GetOnlyOne(int id)
        {
            return Json(bll.Search(x => x.leagueid == id).Select(x => new { x.leagueid, x.leaguephone, x.leagueadress, x.leagueage, x.leaguecardurl, x.leagueemail, x.leaguename, x.leaguenumber, x.leaguepassword, leaguesexID=x.leaguesex, leaguesex = x.leaguesex == 0 ? "男" : "女", x.registertime, x.storeids }).ToList()[0]); 
        }

        //MD5加密方法  32位加密
        public static string MD5Encrypt32(string password)
        {
            string cl = password;
            string pwd = "";
            MD5 md5 = MD5.Create(); //实例化一个md5对像
            // 加密后是一个字节类型的数组，这里要注意编码UTF8/Unicode等的选择　
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
            // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
            for (int i = 0; i < s.Length; i++)
            {
                // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符 
                pwd = pwd + s[i].ToString("X");
            }
            return pwd;
        }
    }
}
